package org.example.src.controller;

import org.example.src.dto.request.WeeklyReportRequest;
import org.example.src.dto.response.ApiResponse;
import org.example.src.dto.response.WeeklyReportResponse;
import org.example.src.entity.WeeklyReport;
import org.example.src.service.WeeklyReportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import jakarta.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/api/weekly-reports")
@CrossOrigin(origins = "*")
public class WeeklyReportController extends BaseController {

    @Autowired
    private WeeklyReportService weeklyReportService;

    @GetMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'TEACHER')")
    public ResponseEntity<ApiResponse<List<WeeklyReport>>> getAllWeeklyReports() {
        List<WeeklyReport> weeklyReports = weeklyReportService.findAllWeeklyReports();
        return success(weeklyReports);
    }

    @GetMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER', 'COMPANY')")
    public ResponseEntity<ApiResponse<WeeklyReport>> getWeeklyReportById(@PathVariable Long id) {
        WeeklyReport weeklyReport = weeklyReportService.findWeeklyReportById(id);
        if (weeklyReport == null) {
            return notFound("周报未找到");
        }
        return success(weeklyReport);
    }

    @PostMapping
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<WeeklyReport>> createWeeklyReport(@Valid @RequestBody WeeklyReportRequest request) {
        WeeklyReport createdWeeklyReport = weeklyReportService.createWeeklyReport(request);
        return created(createdWeeklyReport);
    }

    @PutMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<WeeklyReport>> updateWeeklyReport(@PathVariable Long id, @Valid @RequestBody WeeklyReportRequest request) {
        WeeklyReport updatedWeeklyReport = weeklyReportService.updateWeeklyReport(id, request);
        return success("周报更新成功", updatedWeeklyReport);
    }

    @DeleteMapping("/{id}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT')")
    public ResponseEntity<ApiResponse<Void>> deleteWeeklyReport(@PathVariable Long id) {
        weeklyReportService.deleteWeeklyReport(id);
        return noContent();
    }

    @GetMapping("/student/{studentId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER') or (hasRole('STUDENT') and #studentId == authentication.principal.id)")
    public ResponseEntity<ApiResponse<List<WeeklyReport>>> getWeeklyReportsByStudentId(@PathVariable Long studentId) {
        List<WeeklyReport> weeklyReports = weeklyReportService.findWeeklyReportsByStudentId(studentId);
        return success(weeklyReports);
    }

    @GetMapping("/application/{applicationId}")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'COMPANY', 'TEACHER')")
    public ResponseEntity<ApiResponse<List<WeeklyReport>>> getWeeklyReportsByApplicationId(@PathVariable Long applicationId) {
        List<WeeklyReport> weeklyReports = weeklyReportService.findWeeklyReportsByApplicationId(applicationId);
        return success(weeklyReports);
    }

    @GetMapping("/student/{studentId}/responses")
    @PreAuthorize("hasAnyRole('ADMIN', 'STUDENT', 'TEACHER') or (hasRole('STUDENT') and #studentId == authentication.principal.id)")
    public ResponseEntity<ApiResponse<List<WeeklyReportResponse>>> getWeeklyReportResponsesByStudentId(@PathVariable Long studentId) {
        List<WeeklyReportResponse> weeklyReports = weeklyReportService.getWeeklyReportsByStudentId(studentId);
        return success(weeklyReports);
    }
}
